﻿using System;
using System.Collections.Generic;
using System.Text;
using Models;
using Repositories;
using Services.IService;
using Repositories.IRepository;
using MySqlSugar;
using System.Linq.Expressions;
using Utility;

namespace Services.Service
{
    public class RoleService : IRoleService
    {

        private readonly IRoleRepository _repository;
        private readonly IRolemenuRepository _rolemenurepository;
        private readonly IMenuRepository _menurepository;
        public RoleService(IRoleRepository repository, IRolemenuRepository RolemenuRepository, IMenuRepository MenuRepository)
        {
            _repository = repository;
            _rolemenurepository = RolemenuRepository;
            _menurepository = MenuRepository;
        }

        #region 查询

        /// <summary>
        /// 查询单条根据主键
        /// </summary>
        /// <returns></returns>
        public RoleModel GetById(int? id)
        {
            return _repository.GetById(id);
        }

        /// <summary>
        /// 查询所有
        /// </summary>
        /// <returns></returns>
        public List<RoleModel> GetList()
        {
            return _repository.GetList().ToList();
        }

        /// <summary>
        /// 根据条件查询
        /// </summary>
        /// <returns></returns>
        public List<RoleModel> GetList(Expression<Func<RoleModel, bool>> where)
        {
            return _repository.GetList(where).ToList();
        }

        /// <summary>
        /// 按页查询
        /// </summary>
        public List<RoleModel> GetPageList(Expression<Func<RoleModel, bool>> where, int pageIndex, int pageSize, string OrderFields)
        {
            return _repository.GetPageList(where, pageIndex, pageSize, OrderFields).ToList();
        }
        #endregion

        #region 删除
        /// <summary>
        /// 根据主键删除
        /// </summary>
        public bool DeleteById(int id)
        {
            return _repository.DeleteById(id);
        }

        /// <summary>
        /// 根据主键组批量删除
        /// </summary>
        public bool DeleteById(int[] ids)
        {
            return _repository.DeleteById(ids);
        }

        /// <summary>
        /// 根据实体删除【一定要带主键！！！】
        /// </summary>
        public bool DeleteByModel(RoleModel model)
        {
            return _repository.DeleteByModel(model);
        }

        #endregion

        #region 新增
        /// <summary>
        /// 强制新增，不管主键
        /// </summary>
        public int Insert(RoleModel model)
        {
            return _repository.Insert(model);
        }

        /// <summary>
        /// 批量插入 适合海量数据插入
        /// </summary>
        public bool InsertRange(List<RoleModel> list)
        {
            return _repository.InsertRange(list);
        }

        #endregion

        #region 修改
        /// <summary>
        /// 根据实体更新【主键一定要有！！！】
        /// </summary>
        public bool Update(RoleModel model)
        {
            return _repository.Update(model);
        }

        /// <summary>
        /// 10条以上批量更新
        /// </summary>
        public bool UpdateRange(List<RoleModel> list)
        {
            return _repository.UpdateRange(list);
        }
        #endregion


        /// <summary>
        /// 获取权限菜单json string
        /// </summary>
        /// <param name="RoleId">登录用户的角色ID</param>
        /// <param name="menurootid">根目录ID</param>
        /// <returns></returns>
        public string GetRoleMenuHtml(int RoleId, int menurootid)
        {
            StringBuilder menuhtml = new StringBuilder();

            Expression<Func<RolemenuModel, bool>> Rolemenuwhere = LinqUtil.True<RolemenuModel>();
            Rolemenuwhere = Rolemenuwhere.AndAlso(i => i.Role_Id == RoleId && i.Disable == 1);
            Queryable<RolemenuModel> list = _rolemenurepository.GetList(Rolemenuwhere);//获取角色对应菜单[可用]权限
            Expression<Func<MenuModel, bool>> menuwhere = LinqUtil.True<MenuModel>();
            menuwhere = menuwhere.AndAlso(i => i.Parent_Id == menurootid && i.Disable == 1);
            List<MenuModel> firstlist = _menurepository.GetList(menuwhere).OrderBy(i => i.Menu_rank).ToList();//根据一级目录ID获取二级目录[可用]菜单,并排序
            menuhtml.Append("[");
            int y = 0;
            foreach (MenuModel menu1 in firstlist)
            {
                if (list.Where(i => i.Menu_Id == menu1.Id).Count() > 0)//判断对应菜单，角色是否有 可用权限
                {
                    menuhtml.Append("{");
                    menuhtml.Append("\"name\": \"" + menu1.Menu_name + "\",");
                    menuhtml.Append("\"children\": [");

                    Expression<Func<MenuModel, bool>> menuwhere2 = LinqUtil.True<MenuModel>();
                    menuwhere2 = menuwhere2.AndAlso(i => i.Parent_Id == menu1.Id && i.Disable == 1);
                    List<MenuModel> secondlist = _menurepository.GetList(menuwhere2).OrderBy(i => i.Menu_rank).ToList();//根据二级目录ID获取三级目录[可用]菜单,并排序
                    int x = 0;
                    foreach (MenuModel menu2 in secondlist)
                    {
                        if (list.Where(i => i.Menu_Id == menu2.Id).Count() > 0)//判断对应菜单，角色是否有 可用权限
                        {
                            menuhtml.Append("{");
                            menuhtml.Append("\"id\": \"" + menu2.Menu_idname + "\",");
                            menuhtml.Append("\"name\": \"" + menu2.Menu_name + "\",");
                            menuhtml.Append("\"target\": \"" + menu2.Menu_target + "\",");
                            menuhtml.Append("\"url\": \"" + menu2.Menu_url + "\"");
                            menuhtml.Append("}");
                            menuhtml.Append(",");
                            x++;
                        }

                    }
                    if (x > 0)
                        menuhtml.Remove(menuhtml.ToString().LastIndexOf(','), 1);
                    menuhtml.Append("]");
                    menuhtml.Append("}");
                    menuhtml.Append(",");
                    y++;
                }

            }
            if (y > 0)
                menuhtml.Remove(menuhtml.ToString().LastIndexOf(','), 1);
            menuhtml.Append("]");
            return menuhtml.ToString();
        }

        /// <summary>
        /// 根据角色ID获取可用根目录
        /// </summary>
        /// <param name="RoleId"></param>
        /// <returns></returns>
        public string GetRoleRootMenu(int RoleId)
        {
            StringBuilder menuhtml = new StringBuilder();

            Expression<Func<RolemenuModel, bool>> Rolemenuwhere = LinqUtil.True<RolemenuModel>();
            Rolemenuwhere = Rolemenuwhere.AndAlso(i => i.Role_Id == RoleId && i.Disable == 1);
            List<RolemenuModel> list = _rolemenurepository.GetList(Rolemenuwhere).ToList();//获取角色对应菜单[可用]权限
            
            Expression<Func<MenuModel, bool>> menuwhere = LinqUtil.True<MenuModel>();
            menuwhere = menuwhere.AndAlso(i => i.Parent_Id == 0 && i.Disable == 1);
            List<MenuModel> rootlist = _menurepository.GetList(menuwhere).OrderBy(i => i.Menu_rank).ToList();//获取根目录[可用]菜单,并排序
            int x = 0;
            foreach (MenuModel menu in rootlist)
            {
                if (x == 0)
                    menuhtml.Append("<li class=\"active\">");
                else
                    menuhtml.Append("<li>");

                menuhtml.Append("    <a href=\"/home/homeMenu/" + menu.Id.ToString() + "\" data-toggle=\"sidenav\" data-id-key=\"targetid\">" + menu.Menu_name + "</a>");
                menuhtml.Append("</li>");
                x++;
            }
            return menuhtml.ToString();
        }
    }
}
